<template>
  <div class="groups-container">
    <div class="groups-header">
      <h1>群组管理</h1>
      <div class="header-actions">
        <el-button type="primary" @click="showCreateGroupDialog">创建群组</el-button>
        <el-button @click="searchGroup">搜索群组</el-button>
        <el-button @click="viewJoinRequests">入群申请</el-button>
      </div>
    </div>
    
    <div class="groups-content">
      <div class="groups-section">
        <h2>我创建的群组</h2>
        <div class="groups-list">
          <div class="group-item" v-for="group in myGroups" :key="group.id">
            <img :src="group.avatar" :alt="group.name" class="group-avatar" />
            <div class="group-info">
              <div class="group-name">{{ group.name }}</div>
              <div class="group-details">
                <div class="group-meta">
                  <span>创建时间：{{ group.createTime }}</span>
                  <span>人数：{{ group.memberCount }}/{{ group.maxMembers }}</span>
                </div>
                <div class="group-meta">
                  <span>群主：{{ group.owner }}</span>
                  <span>状态：{{ group.status }}</span>
                </div>
              </div>
            </div>
            <div class="group-actions">
              <el-button size="small" @click="chatWithGroup(group)">聊天</el-button>
              <el-button size="small" @click="manageGroup(group)">管理</el-button>
              <el-button size="small" @click="exitGroup(group)">退出</el-button>
            </div>
          </div>
        </div>
      </div>
      
      <div class="groups-section">
        <h2>我加入的群组</h2>
        <div class="groups-list">
          <div class="group-item" v-for="group in joinedGroups" :key="group.id">
            <img :src="group.avatar" :alt="group.name" class="group-avatar" />
            <div class="group-info">
              <div class="group-name">{{ group.name }}</div>
              <div class="group-details">
                <div class="group-meta">
                  <span>创建时间：{{ group.createTime }}</span>
                  <span>人数：{{ group.memberCount }}/{{ group.maxMembers }}</span>
                </div>
                <div class="group-meta">
                  <span>群主：{{ group.owner }}</span>
                  <span>状态：{{ group.status }}</span>
                </div>
              </div>
            </div>
            <div class="group-actions">
              <el-button size="small" @click="chatWithGroup(group)">聊天</el-button>
              <el-button size="small" @click="exitGroup(group)">退出</el-button>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 创建群组对话框 -->
    <el-dialog title="创建群组" v-model="createGroupDialogVisible" width="500px">
      <el-form :model="newGroup" label-width="80px">
        <el-form-item label="群组名称">
          <el-input v-model="newGroup.name" placeholder="请输入群组名称"></el-input>
        </el-form-item>
        <el-form-item label="群组描述">
          <el-input 
            v-model="newGroup.description" 
            type="textarea" 
            placeholder="请输入群组描述"
            :rows="3"
          ></el-input>
        </el-form-item>
        <el-form-item label="群组头像">
          <el-upload
            class="avatar-uploader"
            action="#"
            :auto-upload="false"
            :show-file-list="false"
          >
            <img v-if="newGroup.avatar" :src="newGroup.avatar" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="初始成员">
          <el-select 
            v-model="newGroup.members" 
            multiple 
            placeholder="请选择初始成员"
            style="width: 100%"
          >
            <el-option
              v-for="friend in friends"
              :key="friend.id"
              :label="friend.name"
              :value="friend.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="createGroupDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="createGroup">创建</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Groups",
  data() {
    return {
      createGroupDialogVisible: false,
      newGroup: {
        name: "",
        description: "",
        avatar: "",
        members: []
      },
      friends: [
        { id: 1, name: "张三" },
        { id: 2, name: "李四" },
        { id: 3, name: "王五" },
        { id: 4, name: "赵六" },
        { id: 5, name: "钱七" }
      ],
      myGroups: [
        {
          id: 1,
          name: "技术交流群",
          avatar: require('@/assets/default-avatar.png'),
          createTime: "2024-01-01",
          memberCount: 15,
          maxMembers: 100,
          owner: "我",
          status: "正常"
        },
        {
          id: 2,
          name: "同学群",
          avatar: require('@/assets/default-avatar.png'),
          createTime: "2023-12-15",
          memberCount: 25,
          maxMembers: 50,
          owner: "我",
          status: "正常"
        }
      ],
      joinedGroups: [
        {
          id: 3,
          name: "前端技术分享群",
          avatar: require('@/assets/default-avatar.png'),
          createTime: "2024-01-10",
          memberCount: 32,
          maxMembers: 200,
          owner: "技术总监",
          status: "正常"
        }
      ]
    };
  },
  methods: {
    showCreateGroupDialog() {
      this.createGroupDialogVisible = true;
    },
    
    createGroup() {
      if (!this.newGroup.name) {
        this.$message.warning("请输入群组名称");
        return;
      }
      
      if (this.newGroup.members.length === 0) {
        this.$message.warning("请选择至少一个成员");
        return;
      }
      
      // 模拟创建群组
      this.$message.success(`群组 "${this.newGroup.name}" 创建成功`);
      this.createGroupDialogVisible = false;
      
      // 重置表单
      this.newGroup = {
        name: "",
        description: "",
        avatar: "",
        members: []
      };
    },
    
    searchGroup() {
      this.$message.info("搜索群组功能待实现");
    },
    
    viewJoinRequests() {
      this.$message.info("入群申请功能待实现");
    },
    
    chatWithGroup(group) {
      this.$message.info(`与群组 "${group.name}" 开始聊天`);
      // 这里应该跳转到群聊页面
    },
    
    manageGroup(group) {
      this.$message.info(`管理群组 "${group.name}"`);
      // 这里应该跳转到群组管理页面
    },
    
    exitGroup(group) {
      this.$confirm(`确定要退出群组 "${group.name}" 吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message.success(`已退出群组 "${group.name}"`);
        // 这里应该调用API退出群组
      }).catch(() => {
        // 用户取消操作
      });
    }
  }
};
</script>

<style scoped>
.groups-container {
  padding: 20px;
  height: 100%;
  overflow-y: auto;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  min-height: 100vh;
}

.groups-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.groups-header h1 {
  margin: 0;
  font-size: 24px;
  color: #333;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.groups-content {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 15px;
  padding: 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
}

.groups-section {
  margin-bottom: 30px;
}

.groups-section h2 {
  font-size: 18px;
  color: #333;
  margin-bottom: 15px;
}

.group-item {
  display: flex;
  align-items: center;
  padding: 15px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 10px;
  margin-bottom: 15px;
  background: rgba(255, 255, 255, 0.3);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.group-item:hover {
  background: rgba(255, 255, 255, 0.5);
  transform: translateY(-2px);
  box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
}

.group-avatar {
  width: 60px;
  height: 60px;
  border-radius: 10px;
  margin-right: 15px;
  object-fit: cover;
  border: 2px solid rgba(255, 255, 255, 0.3);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.group-info {
  flex: 1;
}

.group-name {
  font-size: 16px;
  font-weight: 500;
  color: #333;
  margin-bottom: 10px;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.group-details {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.group-meta {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: #666;
  background: rgba(255, 255, 255, 0.3);
  padding: 4px 8px;
  border-radius: 6px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.group-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.group-actions .el-button {
  background: rgba(255, 255, 255, 0.3);
  border: none;
  color: #333;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.group-actions .el-button:hover {
  background: rgba(255, 255, 255, 0.5);
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
}

.group-actions .el-button--primary {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: #1890ff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  text-align: center;
  line-height: 100px;
}

.avatar {
  width: 100px;
  height: 100px;
  display: block;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

/* 移动端优化 */
@media (max-width: 767px) {
  .groups-container {
    padding: var(--spacing-sm);
  }
  
  .groups-header {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--spacing-sm);
    margin-bottom: var(--spacing-sm);
  }
  
  .groups-header h1 {
    font-size: 20px;
  }
  
  .header-actions {
    width: 100%;
  }
  
  .header-actions .el-button {
    flex: 1;
    font-size: 13px;
    padding: 8px 12px;
  }
  
  .groups-content {
    padding: var(--spacing-sm);
  }
  
  .groups-section {
    margin-bottom: var(--spacing-md);
  }
  
  .groups-section h2 {
    font-size: 16px;
    margin-bottom: var(--spacing-sm);
  }
  
  .group-item {
    flex-direction: column;
    align-items: flex-start;
    padding: var(--spacing-sm);
  }
  
  .group-avatar {
    width: 50px;
    height: 50px;
    margin-right: 0;
    margin-bottom: var(--spacing-sm);
  }
  
  .group-info {
    width: 100%;
    margin-bottom: var(--spacing-sm);
  }
  
  .group-name {
    font-size: 15px;
    margin-bottom: var(--spacing-xs);
  }
  
  .group-meta {
    flex-direction: column;
    gap: var(--spacing-xs);
  }
  
  .group-actions {
    align-self: flex-end;
  }
  
  .group-actions .el-button {
    font-size: 12px;
    padding: 4px 8px;
  }
  
  .avatar-uploader-icon {
    font-size: 24px;
    width: 80px;
    height: 80px;
    line-height: 80px;
  }
  
  .avatar {
    width: 80px;
    height: 80px;
  }
}
</style>